package com.mars.web.rest.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mars.base.common.exception.AppException;
import com.mars.base.common.utils.web.HttpCode;
import com.mars.base.common.utils.web.ResponseResult;
import com.mars.base.web.BaseController;
import com.mars.manager.entity.Menu;
import com.mars.manager.service.MenuService;
import com.mars.manager.user.exception.UserBizException;

@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController {

	@Autowired
	private MenuService service;
	
	@PostMapping(value = "/add")
	public Object add(Menu entity) {
		try {
			if(null != entity.getColor()) {
				entity.setColor(entity.getColor().toUpperCase());
			}
			this.service.insertSelective(entity) ;
		} catch (AppException e) {
			return ResponseResult.addFailed();
		}
		return ResponseResult.addOk();
	}
	
	@GetMapping(value = "/delete/{ids}")
	public Object delete(@PathVariable List<Long> ids) {
		try {
			this.service.deleteByIds(ids);
		} catch (UserBizException e) {
			return ResponseResult.deleteFailed();
		}
		return ResponseResult.deleteOk();
	}
	
	@PostMapping(value = "/update")
	public Object update(Menu entity) {
		try {
			if(null != entity.getColor()) {
				entity.setColor(entity.getColor().toUpperCase());
			}
			this.service.updateSelective(entity) ;
		} catch (AppException e) {
			return ResponseResult.updateFailed();
		}
		return ResponseResult.updateOk();
	}
	
	@GetMapping(value = "/get/{id}")
	public Object get(@PathVariable Long id) {
		try {
			return this.service.selectById(id) ;
		} catch (AppException e) {
			return ResponseResult.loadFailed();
		}
	}
	
	@GetMapping(value = "/list")
	public Object list(Menu entity) {
		return ResponseResult.loadOk(this.service.selectList(entity));
	}
	
	@GetMapping(value = "/rootNode")
	public Object rootNode(Menu entity) {
		return this.service.loadRootNode() ;
	}
	
	@GetMapping(value = "/childNode/{pid}")
	public Object rootNode(@PathVariable Long pid) {
		return this.service.loadChildrenNode(pid) ;
	}
	
	@GetMapping(value = "/tree")
	public Object tree(Menu entity) {
		return this.service.tree();
	}
	
	@GetMapping(value = "/treegrid")
	public Object treegrid(Menu entity) {
		return this.service.treegrid();
	}

	@GetMapping(value = "/clean")
	public Object clean() {
		Long count = this.service.cleanCache() ;
		return ResponseResult.build(HttpCode.CLEAR_CACHE, count);
	}

}
